using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class CharacterCreatorCanvas : MonoBehaviour
{
	public RectTransform CharacterBadgePrefab;

	public RectTransform Canvas;

	[HideInInspector]
	public CharacterSaveData[] Characters;

	private Vector2 scrollPosition;

	private int indexCharacter;

	public Transform previewSpot;

	private GameObject characterPreviewer;

	[HideInInspector]
	public CharacterSaveData characterLoaded;

	private string characterName="Player";

	private GameObject newcharacter;

    private void Start()
	{
        Setup();
		StartCoroutine(LoadCharacters());
		newcharacter = GameObject.Find("NewCharacter");
	}

	public void Setup()
	{
		ClearCanvas();
		indexCharacter = PlayerPrefs.GetInt("INDEX_CRE_CHAR");
	}

	private void ClearCanvas()
	{
		if (!(Canvas == null))
		{
			IEnumerator enumerator = Canvas.transform.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					Transform transform = (Transform)enumerator.Current;
					UnityEngine.Object.Destroy(transform.gameObject);
				}
			}
			finally
			{
				IDisposable disposable;
				if ((disposable = (enumerator as IDisposable)) != null)
				{
					disposable.Dispose();
				}
			}
		}
	}

	public IEnumerator LoadCharacters()
	{
		if (!UnitZ.playerSave)
		{
			yield break;
		}
		Characters = UnitZ.playerSave.LoadAllCharacters();
		while (Characters == null)
		{
			yield return new WaitForEndOfFrame();
		}
		if (Characters.Length > 0)
		{
			if (indexCharacter >= Characters.Length)
			{
				indexCharacter = Characters.Length - 1;
			}
			if (indexCharacter < 0)
			{
				indexCharacter = 0;
			}
			characterLoaded = Characters[indexCharacter];
			if (!UnitZ.characterManager)
			{
				yield break;
			}
			UnitZ.characterManager.SetupCharacter(characterLoaded);
			if (UnitZ.characterManager.CharacterSelected != null)
			{
				if (characterPreviewer != null)
				{
					UnityEngine.Object.Destroy(characterPreviewer);
				}
				characterPreviewer = UnityEngine.Object.Instantiate(UnitZ.characterManager.CharacterSelected, previewSpot.position, previewSpot.rotation);
				if ((bool)characterPreviewer.GetComponent<CharacterMotor>())
				{
					characterPreviewer.GetComponent<CharacterMotor>().enabled = false;
				}
				if ((bool)characterPreviewer.GetComponent<HumanCharacter>())
				{
					characterPreviewer.GetComponent<HumanCharacter>().enabled = false;
				}
				if ((bool)characterPreviewer.GetComponent<CharacterInventory>())
				{
					characterPreviewer.GetComponent<CharacterInventory>().SetupStarterItem();
					characterPreviewer.GetComponent<CharacterInventory>().enabled = false;
				}
				if ((bool)characterPreviewer.GetComponent<CharacterAnimation>())
				{
					characterPreviewer.GetComponent<CharacterAnimation>().enabled = false;
				}
				if ((bool)characterPreviewer.GetComponent<CharacterFootStep>())
				{
					characterPreviewer.GetComponent<CharacterFootStep>().enabled = false;
				}
				if ((bool)characterPreviewer.GetComponent<CharacterDriver>())
				{
					characterPreviewer.GetComponent<CharacterDriver>().enabled = false;
				}
				if ((bool)characterPreviewer.GetComponent<FPSController>())
				{
					characterPreviewer.GetComponent<FPSController>().enabled = false;
				}
				if ((bool)characterPreviewer.GetComponent<CharacterItemDroper>())
				{
					characterPreviewer.GetComponent<CharacterItemDroper>().enabled = false;
				}
				characterPreviewer.GetComponent<PlayerView>().FPSCamera.gameObject.SetActive(value: false);
			}
		}
		else
		{
            //MainMenuManager mainMenuManager = (MainMenuManager)UnityEngine.Object.FindObjectOfType(typeof(MainMenuManager));
            //if ((bool)mainMenuManager)
            //{
            //	mainMenuManager.OpenPanelByName("CreateCharacter");
            //}
            newcharacter.SetActive(false);
            UnitZ.characterManager.CreateCharacter(characterName);
        }
	}

	public void DrawCharactersToCanvas()
	{
		if (Canvas == null || CharacterBadgePrefab == null || Characters == null)
		{
			return;
		}
		ClearCanvas();
		for (int i = 0; i < Characters.Length; i++)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(CharacterBadgePrefab.gameObject, Vector3.zero, Quaternion.identity);
			gameObject.transform.SetParent(Canvas.transform);
			CharacterBadge component = gameObject.GetComponent<CharacterBadge>();
			RectTransform component2 = gameObject.GetComponent<RectTransform>();
			if ((bool)component2)
			{
				RectTransform rectTransform = component2;
				Vector2 sizeDelta = CharacterBadgePrefab.sizeDelta;
				rectTransform.anchoredPosition = new Vector2(5f, 0f - (sizeDelta.y + 5f) * (float)i);
				component2.localScale = CharacterBadgePrefab.gameObject.transform.localScale;
			}
			if ((bool)component)
			{
				component.Index = i;
				component.CharacterData = Characters[i];
				if ((bool)UnitZ.characterManager && UnitZ.characterManager.CharacterPresets.Length > 0 && Characters[i].CharacterIndex < UnitZ.characterManager.CharacterPresets.Length)
				{
					component.GUIImage.texture = UnitZ.characterManager.CharacterPresets[Characters[i].CharacterIndex].Icon;
				}
				component.GUIName.text = Characters[i].PlayerName;
				component.CharacterCreatorS = this;
				component.name = Characters[i].PlayerName;
			}
		}
		RectTransform canvas = Canvas;
		Vector2 sizeDelta2 = Canvas.sizeDelta;
		float x = sizeDelta2.x;
		Vector2 sizeDelta3 = CharacterBadgePrefab.sizeDelta;
		canvas.sizeDelta = new Vector2(x, (sizeDelta3.y + 5f) * (float)Characters.Length);
	}

	public void CreateCharacter(Text textName)
	{
		if ((bool)UnitZ.characterManager && (bool)textName && UnitZ.characterManager.CreateCharacter(textName.text))
		{
			Setup();
			StartCoroutine(LoadCharacters());
			MainMenuManager mainMenuManager = (MainMenuManager)UnityEngine.Object.FindObjectOfType(typeof(MainMenuManager));
			if ((bool)mainMenuManager)
			{
				mainMenuManager.OpenPanelByNameNoPreviousSave("Home");
			}
		}
	}



	public void ChangeCharacter(int index)
	{
		if ((bool)UnitZ.playerSave)
		{
			ClearCanvas();
			characterLoaded.CharacterIndex = index;
			if (UnitZ.playerSave.UpdateCharacter(characterLoaded))
			{
                StartCoroutine(LoadCharacters());
			}
		}
	}

	public void SelectCharacter(CharacterSaveData character)
	{
		if ((bool)UnitZ.characterManager)
		{
			UnitZ.characterManager.SetupCharacter(character);
		}
		MainMenuManager mainMenuManager = (MainMenuManager)UnityEngine.Object.FindObjectOfType(typeof(MainMenuManager));
		if ((bool)mainMenuManager)
		{
			mainMenuManager.OpenPanelByName("EnterWorld");
		}
	}

	public void SetCharacter()
	{
		if ((bool)UnitZ.characterManager)
		{
			UnitZ.characterManager.SetCharacter();
		}
	}

	public void SelectCreateCharacter(int index)
	{
		if ((bool)UnitZ.characterManager)
		{
			UnitZ.characterManager.SelectCreateCharacter(index);
		}
	}

	public void RemoveCharacter(int index)
	{
		if ((bool)UnitZ.characterManager)
		{
			UnitZ.characterManager.RemoveCharacter(Characters[index]);
			StartCoroutine(LoadCharacters());
		}
	}
}
